package videoCapture;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import javax.imageio.ImageIO;

public class UdpClient {
	private static final int PORT = 4561;
    private DatagramSocket dataSocket;
    private DatagramPacket dataPacket;
    private byte sendDataByte[];
    private String sendStr;

    public UdpClient() {
        Init();
    }

    public void Init() {
        try {
            // 指定端口号，避免与其他应用程序发生冲突

            dataSocket = new DatagramSocket(PORT);
        //    sendDataByte = new byte[1024];
            sendStr = "";
 //           sendDataByte = sendStr.getBytes();
            BufferedImage bimg = ImageIO.read(new File("test.jpg"));
            ByteArrayOutputStream out = new ByteArrayOutputStream();  
            ImageIO.write(bimg, "jpg", out);  
            sendDataByte = out.toByteArray();
            
            int index = 0;
            int size =1024;
            int bufSize=0;
            while(index < sendDataByte.length){
            	byte[] t = new byte[1024];
            	int i;
            	for(i = 0; i < size && (index + i) < sendDataByte.length;i++){
            		System.out.println("client "+i);
            		t[i] = sendDataByte[index+i];
            	}
            	index += i;
            	bufSize += i;
	            dataPacket = new DatagramPacket(t, i,
	                    InetAddress.getByName("localhost"), UdpServer.PORT);
	            dataSocket.send(dataPacket);
            }
            System.out.println("client "+bufSize);
  //          dataSocket.send(new  DatagramPacket(sendStr.getBytes(), sendStr.length(),InetAddress.getByName("localhost"), UdpServer.PORT));
        } catch (SocketException se) {
            se.printStackTrace();
        } catch (IOException ie) {
            ie.printStackTrace();
        }
    }

    public static void main(String args[]) {
    	
        new UdpClient();
    }
}
